﻿Shader "Lch/SimpleEffect"
{
    Properties
    {
        _Color("Color", Color)= (1,1,1,1)
        _BaseMap("BaseMap", 2D) = "white" {}
        _BaseSpeed("主贴图流动速度",Vector) = (0.01,0.02,2,2)
 
        [Toggle]_CustomReflect("自定义反射球", float) = 1
        _SkyboxMip("反射球模糊",Range(0,8)) = 0
        _SkyBoxPower("金属亮度",Range(1,3)) = 1
        _BumpSpeed("法线流动速度",Vector) = (0.01,0.02,-0.36,-0.23)
        
        _BumpMap("_BumpMap", 2D) = "bump" {}
        
        _BumpScale("_BumpScale", Range(0,2)) =1

        //[Toggle]_Disturbance("扰动", float) = 1
        [HDR]_SpecularColor("高光色(RGB) 高光润度(A)", Color) = (1,1,1,1)
        [HDR]_EmissionColor("EmissionColor", Color) = (0,0,0,1)
 
        [LCHEnum(LCHblendlModel)] _bendModel("混合模式", Int) = 1
 
		[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("源混合 SrcBlend", Float) = 5
		[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("目标混合 DstBlend", Float) = 10
		[Enum(Off, 0, On, 1)]_ZWrite("深度写_ZWrite", float) = 1
		[Enum(UnityEngine.Rendering.CullMode)]_Cull("剔除模式", float) = 2

		_OffsetFactor("双材质防穿插,越小越前", Float) = 0
    }
    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalPipeline"
            "RenderType"="Transparent"
            "IgnoreProjector"="True"
            "Queue"="Transparent"
        }

        Pass
        {
            Blend[_SrcBlend][_DstBlend]
            ZWrite[_ZWrite]
            Cull[_Cull]
            //Offset -10, 1
            Offset [_OffsetFactor], [_OffsetFactor]
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"

            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"

            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"

 

            half3 SampleNormal(float2 uv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = half(1.0))
            {
       
                half4 n = SAMPLE_TEXTURE2D(bumpMap, sampler_bumpMap, uv);
               
                    return UnpackNormalScale(n, scale);
             
            
            }
                        //GetOddNegativeScale
            struct Attributes
            {
                float3 positionOS : POSITION;
                float2 uv : TEXCOORD0;
                float3 normalOS : NORMAL;
                float4 tangentOS : TANGENT;
                float4 color: COLOR;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 normalWS : TEXCOORD1;
                float4 tangentWS : TEXCOORD2;
                float3 positionWS: TEXCOORD3;
                float4 color: TEXCOORD4;
                float4 screenPosition : TEXCOORD5;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _Color;
                float4 _BaseMap_ST;
                half4 _SpecularColor;
                half4 _EmissionColor;
 
                half _SkyBoxPower;

                half _CustomReflect;
                half _SkyboxMip;
                half _BumpScale;
                half4 _BumpSpeed;
                half4 _BaseSpeed;
                //half _Disturbance;
            CBUFFER_END

            SAMPLER(sampler_BaseMap);
            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BumpMap);
            TEXTURE2D(_BumpMap);
            
            float4 ComputeScreenPos(float4 positionCS)
            {
                float4 o = positionCS * 0.5f;
                o.xy = float2(o.x, o.y * _ProjectionParams.x) + o.w;
                o.zw = positionCS.zw;
                return o;
            }
            Varyings vert(Attributes v)
            {
                Varyings o;
                o.color = v.color;
                o.positionCS = TransformObjectToHClip(v.positionOS);
                o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
                o.normalWS = TransformObjectToWorldNormal(v.normalOS);

 
                float sign = v.tangentOS.w * GetOddNegativeScale();
                half4 tangentWS = half4(TransformObjectToWorldNormal(v.tangentOS.xyz), sign);
 
                o.tangentWS = tangentWS;
                o.positionWS= TransformObjectToWorld(v.positionOS);
                o.screenPosition = ComputeScreenPos(o.positionCS);
                return o;
            }


            SAMPLER(sampler_CustomCubeTex);
            TEXTURECUBE(_CustomCubeTex);
            float4 _CustomCubeTex_HDR;

            float3 blend_rnm_unpacked(float3 n1, float3 n2)
            {
                float3 t = n1.xyz + float3( 0,  0, 1);
                float3 u = n2.xyz * float3(-1, -1, 1);
                float3 r = (t/t.z)*dot(t, u) - u;
                return r;
            }
            TEXTURE2D(_CameraOpaqueTexture);
            SAMPLER(sampler_CameraOpaqueTexture);
            half4 frag(Varyings i) : SV_Target
            {
                float2 uv = i.uv;
                float4 vcolor = i.color;
                half4 baseCol = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
                
               
                 
                half3 albedo =   _Color.rgb  ;
                half alpha = baseCol.g*_Color.a*vcolor.g;

                
               

                float sgn = i.tangentWS.w;      // should be either +1 or -1
                float3 bitangent = sgn * cross(i.normalWS.xyz, i.tangentWS.xyz);
                float3x3 tangentToWorld = float3x3(i.tangentWS.xyz, bitangent.xyz, i.normalWS.xyz);
                
                float2 uv0 = uv+_BumpSpeed.xy*_Time.y;
                float2 uv1 = uv+_BumpSpeed.zw*_Time.y;
                float3 normalTS0 =  SampleNormal(uv0, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
                float3 normalTS1 =   SampleNormal(uv1, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
                float3 normalTS =    blend_rnm_unpacked(normalTS0,normalTS1);
                float3 normalWS = TransformTangentToWorld( normalTS, tangentToWorld);


               
               
      ;
				//环境光
                half3 ambient = _GlossyEnvironmentColor.rgb;

                half3 n = normalize(normalWS);
                half3 l = normalize(_MainLightPosition.xyz);
                half3 v = normalize(_WorldSpaceCameraPos.xyz - i.positionWS);
                half3 h = saturate(normalize(l + v));

                half nDotV = dot(n,v);
                half nDotVInv = 1.0-nDotV;
               
                //漫反射
                half3 diffuse =  _MainLightColor.rgb * saturate(dot(n, l))  ;

                //把0-1 映射到一定范围
                half smoothness = exp(10 * _SpecularColor.a + 1);
				//高光
                half3 specular = _MainLightColor.rgb * _SpecularColor.rgb * pow(saturate(dot(n, h)), smoothness);

                //自发光
                half3 emission = _EmissionColor.rgb;

                half3 irradiance = 0; 
                if(_CustomReflect)
                {
                        half3 reflectVector = reflect(-v, n);
                       
                    half4 encodedIrradiance = half4(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector, _SkyboxMip));

                    #if defined(UNITY_USE_NATIVE_HDR)
                        irradiance = encodedIrradiance.rgb;
                    #else
                        irradiance = DecodeHDREnvironment(encodedIrradiance, unity_SpecCube0_HDR);
                    #endif
                }
                else
                {
                    half3 reflectVector = reflect(-v, n);
                       
                    half4 encodedIrradiance = half4(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector, _SkyboxMip));

                    #if defined(UNITY_USE_NATIVE_HDR)
                        irradiance = encodedIrradiance.rgb;
                    #else
                        irradiance = DecodeHDREnvironment(encodedIrradiance, unity_SpecCube0_HDR);
                    #endif
                }
                 
                float4 final =  half4(albedo * (  irradiance*_SkyBoxPower *(1.0+nDotVInv)   + specular) + emission, alpha  );
                
                /*if(_Disturbance)
                {
                    float2 screenPos = i.screenPosition.xy / i.screenPosition.w;
                    float4 sreenColor = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenPos+normalTS.xy*0.05*alpha);
                    final.rgb = lerp(sreenColor,final.rgb,final.a);
                    final.a = 1.0;
                }*/
                
                return final;

                   
                
				
            }
            ENDHLSL
        }
    }
    FallBack "Hidden/Shader Graph/FallbackError"
    CustomEditor "LCHShaderGUIBase" 
}